#!/bin/sh
#BK called from /root/.xinitrc
#v410 remove icons when drives removed.
#v410 omit icon if optical drive; defer it to pup_event_frontend_d
#w014 bugfix, refresh icons if removable drive swapped when PC turned off.
#120131 rodin.s: internationalized. See warning about "Size:" translation.
#120213 /var/local/pup_event_icon_change_flag path changed from /tmp.
#120222 internationalization introduced bug, fixed it (caused drive icons to redraw every time X starts).

# i18n gettext
TEXTDOMAIN=clean_desk_icons
export TEXTDOMAIN
. gettext.sh

export LANG=C
. /etc/eventmanager #has RAMSAVEINTERVAL, HOTPLUGNOISY, HOTPLUGON, FLOPPYICON.
. /etc/rc.d/functions_x

ePUPPYPIN="`grep -v '/root/.pup_event/drive_' /root/Choices/ROX-Filer/PuppyPin | grep -v '</pinboard>'`"

#v403 /usr/sbin/eventmanager creates this file if all icons need to be rebuilt...
#120213 note, /var/local/pup_event_icon_change_flag is written to by /usr/sbin/xorgwizard, video-wizard, eventmanager.
if [ -f /var/local/pup_event_icon_change_flag ];then #120213 path changed from /tmp
 rm -f /var/local/pup_event_icon_change_flag
 echo "$ePUPPYPIN" >  /root/Choices/ROX-Filer/PuppyPin
 echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
 rm -rf /root/.pup_event/drive_* 2>/dev/null
 exit
fi

#remove all invalid drive icons off desktop...
echo -n "" > /tmp/pup_event_ok_pin
if [ "$HOTPLUGON" = "false" ];then
 #leave single 'drives' icon on desktop...
 grep '/root/.pup_event/drive_drives' /root/Choices/ROX-Filer/PuppyPin >> /tmp/pup_event_ok_pin
 rm -rf /root/.pup_event/drive_[^d]* 2>/dev/null #delete all except drive_drives.
else

 #v410 Delete drive_ directories for removed drives...
 #note, this will not detect removed optical and floppy discs (see /sbin/pup_event_frontend_d).
 #Get the directory names for the drives only, not the partitions, to avoid redundant iterations through the for-loop.
 DIR_DRVS="`ls -1 /root/.pup_event | sed 's/drive_//' | grep -E "^hd.$|^sd.$|^sr|^mmcblk.$|^nvme.$" | tr '\n' ' '`"
 for ONEDRV in $DIR_DRVS
 do
  [ ! -d /root/.pup_event/drive_$ONEDRV ] && continue
  [ ! -e /sys/block/${ONEDRV} ] && rm -rf /root/.pup_event/drive_${ONEDRV}*
 done
 
 for ONEDRV in `ls -1 /sys/block | grep -vE 'loop|ram|zram' | tr '\n' ' '`
 do
  odPATTERN="/root/.pup_event/drive_${ONEDRV}"
  OKDRV="`grep "$odPATTERN" /root/Choices/ROX-Filer/PuppyPin`"
  if [ "$OKDRV" = "" ];then
   rm -rf /root/.pup_event/drive_${ONEDRV}* 2>/dev/null
  else
   [ ! -d /root/.pup_event/drive_${ONEDRV} ] && continue #v408
   #omit icon if optical drive; defer it to pup_event_frontend_d
   if fx_drv_is_optical ${ONEDRV} ; then
     continue
   fi
   #120222 above is fixed, however go one step further and eliminate dependency on the translation of "Size:"...
   DRVMODEL1="`grep -o '<Summary>.*</Summary>' /root/.pup_event/drive_${ONEDRV}/AppInfo.xml | cut -f 2-9 -d ':' | cut -f 1 -d '<' | tr -s ' ' | rev | cut -f 3-99 -d ' ' | rev | sed -e 's%^ %%'`"
   #note, this must be same as done in /sbin/probedisk...
   DRVMODEL2="`cat /sys/block/$ONEDRV/device/vendor | tr -s ' '``cat /sys/block/${ONEDRV}/device/model | tr -s ' '`"
   if [ "$DRVMODEL1" != "$DRVMODEL2" ];then
    rm -rf /root/.pup_event/drive_${ONEDRV}* 2>/dev/null
    continue
   fi
   echo "$OKDRV" >> /tmp/pup_event_ok_pin
  fi
 done
 
fi

#if [ "`cat /tmp/pup_event_ok_pin`" != "" ];then
 echo "$ePUPPYPIN" >  /root/Choices/ROX-Filer/PuppyPin
 cat /tmp/pup_event_ok_pin >> /root/Choices/ROX-Filer/PuppyPin
 echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
#fi

###END###
